﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Besture.Bll;
using Besture.Dal;
using Besture.Model;
using Besture.Common;
using Besture.Common.Helper;
using Besture.UI.Common;

namespace Besture.UI.Portal.Pay
{
    public partial class OrderInfo : System.Web.UI.Page
    {
        private int hotelid = 0;
        private int roomid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["ud"] != null)
                {
                    try
                    {
                        string[] arr = Request["ud"].ToString().Split('/');
                        if (arr.Length == 4)
                        {
                            DateTime beginTime = DateTime.Now;
                            DateTime endTime = DateTime.Now.AddDays(1);
                            hotelid = Convert.ToInt32(arr[0]);
                            roomid = Convert.ToInt32(arr[1]);
                            if (arr[2] != "")
                            {
                                beginTime = Convert.ToDateTime(arr[2]);
                            }
                            if (arr[3] != "")
                            {
                                endTime = Convert.ToDateTime(arr[3]);
                            }

                            this.lblBeginTime.Text = beginTime.ToString("yyyy-MM-dd");
                            this.lblEndTime.Text = endTime.ToString("yyyy-MM-dd");
                            this.lblDay1.Text = (endTime - beginTime).Days.ToString() + "晚";
                            this.lblDay2.Text = (endTime - beginTime).Days.ToString() + "晚";

                            GetHotelInfo(hotelid, roomid);
                        }
                        else
                        {
                            FunHelper.Alert("参数错误", "../Hotel/Product.aspx");
                        }
                    }
                    catch
                    {
                        FunHelper.Alert("参数错误", "../Hotel/Product.aspx");
                    }
                }
                else
                {
                    FunHelper.Alert("参数错误", "../Hotel/Product.aspx");
                }
            }
        }

        /// <summary>
        /// 获取酒店信息
        /// </summary>
        /// <param name="hotelid"></param>
        /// <param name="roomid"></param>
        /// <param name="bt"></param>
        /// <param name="et"></param>
        private void GetHotelInfo(int hotelid, int roomid)
        {
            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                OrderDac dac = new OrderDac();
                UserBaseModel ubm = ((UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL]);
                DataSet ds = dac.GetHotelRoomInfo(ubm.UserType, hotelid, roomid, this.lblBeginTime.Text, this.lblEndTime.Text);
                IList<OrderInfo_HotelRoom_Model> list = ModelConvertHelper<OrderInfo_HotelRoom_Model>.ConvertToModelList(ds.Tables[0]);
                IList<OrderInfo_Require_Model> listRequire = ModelConvertHelper<OrderInfo_Require_Model>.ConvertToModelList(ds.Tables[1]);
                
                this.lblHotelName1.Text = list[0].HotelsName;
                this.lblHotelName2.Text = list[0].HotelsName;
                this.lblRoomName.Text = list[0].RoomName;
                this.lblPrice.Text = list.Where(p => p.RoomTime == Convert.ToDateTime(this.lblBeginTime.Text)).Single().Price.ToString("f0");
                this.lblBreakfast.Text = list[0].Breakfast;
                int roomNum = list.Min(p => p.RoomQuantity);
                for (int i = 1; i <= roomNum; i++)
                {
                    this.ddlRoomNum.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                this.ddlArrive.Items.Add(new ListItem("18:00", "18:00"));
                this.ddlArrive.Items.Add(new ListItem("19:00", "18:00"));
                this.ddlArrive.Items.Add(new ListItem("20:00", "18:00"));
                this.ddlArrive.Items.Add(new ListItem("21:00", "18:00"));
                this.ddlArrive.Items.Add(new ListItem("22:00", "18:00"));
                int dayspan = (Convert.ToDateTime(this.lblEndTime.Text) - Convert.ToDateTime(this.lblBeginTime.Text)).Days;
                this.lblTotal.Text = this.lblScore.Text = (Convert.ToInt32(this.lblPrice.Text) * Convert.ToInt32(this.ddlRoomNum.SelectedValue) * dayspan).ToString();

                Repeater1.DataSource = listRequire;
                Repeater1.DataBind();
            }
            else
            {
                FunHelper.Alert("超时，请重新登录", "../UserCenter/Login.aspx");
            }
        }

        /// <summary>
        /// 选择房间，重新计算价格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlRoomNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            int dayspan = (Convert.ToDateTime(this.lblEndTime.Text) - Convert.ToDateTime(this.lblBeginTime.Text)).Days;
            this.lblTotal.Text = this.lblScore.Text = (Convert.ToInt32(this.lblPrice.Text) * Convert.ToInt32(this.ddlRoomNum.SelectedValue) * dayspan).ToString();
        }

        /// <summary>
        /// 确认提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string requireID = "";
            string requireText = "";
            OrderInfo_Order_Model model = new OrderInfo_Order_Model();
            string[] arr = Request["ud"].ToString().Split('/');
            model.HotelID = Convert.ToInt32(arr[0]);
            model.RoomID = Convert.ToInt32(arr[1]);

            //源代码
            //model.HotelID = hotelid;
            //model.RoomID = roomid;

            model.HotelsName = this.lblHotelName1.Text;
            model.RoomName = this.lblRoomName.Text;
            model.Breakfast = this.lblBreakfast.Text;
            model.BeginTime = this.lblBeginTime.Text;
            model.EndTime = this.lblEndTime.Text;
            model.DaySpan = (Convert.ToDateTime(this.lblEndTime.Text) - Convert.ToDateTime(this.lblBeginTime.Text)).Days;
            model.Price = this.lblPrice.Text;
            model.RoomQuantity = this.ddlRoomNum.SelectedValue;
            model.UserName1 = this.txtUserName1.Text;
            model.UserName2 = this.txtUserName2.Text;
            model.Arrive = this.ddlArrive.SelectedValue;
            model.Customer = this.txtCustomer.Text;
            model.Phone = this.txtPhone.Text;
            model.Email = this.txtEmail.Text;
            for (int i = 0; i < this.Repeater1.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)this.Repeater1.Items[i].FindControl("cbRequire");
                if (cb.Checked == true)
                {
                    requireID += cb.ToolTip + ",";
                    requireText += cb.Text + ",";
                }
            }
            model.RequireID = requireID.Trim(',');
            model.RequireText = requireText.Trim(',');
            model.Remark = this.txtRemark.Text;
            model.TotalPrice = this.lblTotal.Text;
            Session["OrderInfo"] = model;
            Response.Redirect("OrderDetail.aspx");
        }
    }
}